package com.yourenbang.lingyun.transaction.web.controller.yxy;

import com.yourenbang.lingyun.core.base.controller.BaseController;
import com.yourenbang.lingyun.core.message.Request;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.domain.CreditBuyOrder;
import com.yourenbang.lingyun.domain.CreditBuyOrderExample;
import com.yourenbang.lingyun.mapper.CreditBuyOrderMapper;
import com.yourenbang.lingyun.transaction.web.domain.dto.yxy.YxyWithdrawalDTO;
import com.yourenbang.lingyun.transaction.web.domain.vo.yxy.YxyWithdrawalVO;
import com.yourenbang.lingyun.transaction.web.service.yxy.YxyWithdrawalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/transaction/yxy/withdrawal")
public class YxyWithdrawalController extends BaseController {

    @Resource
    YxyWithdrawalService yxyWithdrawalService;
    @Resource
    private CreditBuyOrderMapper creditBuyOrderMapper;

    /**
     * 提现黑名单
     * https://huoyan.kaolakuaishou.cn/project/97/interface/api/8718
     */
    @ResponseBody
    @RequestMapping("get/flag")
    public Result getYxyWithdrawalFlag(@RequestBody @Valid Request<YxyWithdrawalDTO> request, BindingResult br) {
        KLog.info("[提现黑名单]入参:{}", request.toString());
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }

        boolean canWithdrawalFlag =  yxyWithdrawalService.getYxyWithdrawalFlag(request.getData().getUserBaseId());

        if(canWithdrawalFlag) {
            // 判断赊购订单
            CreditBuyOrderExample creditBuyOrderExample = new CreditBuyOrderExample();
            creditBuyOrderExample.createCriteria().andUserBaseIdEqualTo(request.getData().getUserBaseId());
            List<CreditBuyOrder> creditBuyOrderList = creditBuyOrderMapper.selectByExample(creditBuyOrderExample);
            if (CollectionUtils.isNotEmpty(creditBuyOrderList)) {
                for (CreditBuyOrder creditBuyOrder : creditBuyOrderList) {
                    if (creditBuyOrder.getTotalAmount().compareTo(creditBuyOrder.getAlreadyReturn()) > 0
                            && creditBuyOrder.getBanWithdrawal().intValue() == 10) {
                        canWithdrawalFlag = false;
                        break;
                    }
                }
            }
        }

        YxyWithdrawalVO vo = new YxyWithdrawalVO();
        vo.setWithdrawalFlag(canWithdrawalFlag);
        return Result.builder().success(vo);

    }

}
